import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.Timer;

public class BananaDragon implements ActionListener, KeyListener, Followable
{

	int x = 0;
	int y = 0;
	Timer t;
	boolean on;
	boolean[] run = new boolean[4];
	BufferedImage bd;
	GroupOfFireballs GoF;

	public BananaDragon(JFrame f){

		try{

			bd = ImageIO.read(new File("src/daREALZbananadragon.png"));
			t = new Timer(10, this);
			f.addKeyListener(this);
			t.start();
			GoF= new GroupOfFireballs();

		}

		catch(IOException e) {

			System.out.println(e.getMessage());
		}
	}

	public Image getImage(){

		return bd;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if(run [0]){
			y-= 5;
		}
		if(run [1]){
			y += 5;
		}
		if(run [2]){
			x += 5; 
		}
		if(run [3]){
			x -= 5;
		}
		if(this.getX() < 0)
		{
			x = 0;
		}

		if(this.getY() < -15)
		{
			y = -15;
		}

		if(this.getX() > 1275 - 150)
		{
			x = 1275 - 150;
		}
		if(this.getY() >835 - 101)
		{
			y = 835 - 101;
		}
	}

	@Override
	public void keyPressed(KeyEvent move) {	

		if(move.getKeyCode() == KeyEvent.VK_UP){
			run [0] = true;

		}
		if(move.getKeyCode() == KeyEvent.VK_DOWN){
			run [1] = true;

		}
		if(move.getKeyCode() == KeyEvent.VK_RIGHT){
			run[2] = true;

		}
		if(move.getKeyCode() == KeyEvent.VK_LEFT){
			run[3] = true;	

		}
		if(move.getKeyCode() == KeyEvent.VK_SPACE){
			GoF.addBall(new fireball(x+75 ,y-68));
		}

	}

	@Override
	public void keyReleased(KeyEvent move) {

		if(move.getKeyCode() == KeyEvent.VK_UP){
			run [0] = false;

		}
		if(move.getKeyCode() == KeyEvent.VK_DOWN){
			run [1] = false;

		}
		if(move.getKeyCode() == KeyEvent.VK_RIGHT){
			run[2] = false;

		}
		if(move.getKeyCode() == KeyEvent.VK_LEFT){
			run[3] = false;	

		}

	}


	public int getX(){
		return x;


	}

	public int getY(){
		return y;


	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}


	public void paint(Graphics2D g2d)
	{
		g2d.drawImage(bd, getX(), getY(), null);
		GoF.paint(g2d);
	}

	public Rectangle2D.Double shape() {

		Rectangle2D.Double rect = new Rectangle2D.Double(x, y, bd.getWidth(), bd.getHeight());
		return rect;


	}


	public fireball Hit(Collidable c)
	{
		
		for(fireball f: GoF.getFireballs() )
		{
			if(f.shape().intersects(c.shape()))
			{
				return f;
				
			}
			
		}
		return null;
	}
	
	public GroupOfFireballs returnGoF()
	{
		return GoF;
	}
	

}
